<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>字典</title>
  </head>
  <body>
    <script>
      /**
       * 封装formatTreeData
       */
      function formatTreeData(arr) {
        // 删除所有的children，以防止多次调用
        arr.forEach((item) => {
          delete item.children;
        });

        // 构建map
        let map = {};
        arr.forEach((c) => {
          map[c.id] = c;
        });

        let treeData = [];

        arr.forEach((c) => {
          const mapItem = map[c.pid];
          if (mapItem) {
            // mapItem 存在，说明当前数据不是顶层数据
            (mapItem.children || (mapItem.children = [])).push(c);
          } else {
            // mapItem 不存在，说明当前数据是顶层数据
            treeData.push(c);
          }
        });
        return treeData;
      }
      const dataList = [
        {
          id: 1,
          pid: 0,
          name: "服装类",
        },
        {
          id: 2,
          pid: 0,
          name: "手机类",
        },
        {
          id: 3,
          pid: 0,
          name: "食品类",
        },
        {
          id: 4,
          pid: 1,
          name: "连衣裙",
        },
        {
          id: 5,
          pid: 2,
          name: "小米手机",
        },
        {
          id: 6,
          pid: 3,
          name: "北方食品",
        },
        {
          id: 7,
          pid: 4,
          name: "女士连衣裙",
        },
        {
          id: 8,
          pid: 6,
          name: "饺子",
        },
        {
          id: 9,
          pid: 7,
          name: "张三",
        },
      ];
      console.log(formatTreeData(dataList));

      let str = "长*宽+输入框+单选*-下拉单选/单选+(面积%布尔)";
      // 2*5+8+3*-6/2+(20%9)
      let obj = {
        长: 2,
        宽: 5,
        输入框: 8,
        单选: 3,
        下拉单选: 6,
        布尔: 9,
        面积: 20,
      };
      const ys = ["+", "-", "*", "/", "%", "(", ")"];
      let strArr = str.split("");
      let arr = []; // 存储公式里的公式符号
      let ysArr = []; // 存储公式里的变量
      strArr.forEach((item) => {
        if (ys.indexOf(item) == -1) {
          if (!arr.length) {
            // 这里0是判断为false的
            arr[arr.length] = ""; // 这里赋值后数组长度变为了1
            arr[arr.length - 1] += item; //这里是两个相邻的是非计算标识 连接起来
          } else {
            arr[arr.length - 1] += item;
          }
        } else {
          ysArr.push(item);
          // 判断
          arr[arr.length] = "";
        }
      });
      let num = "";
      ysArr.forEach((item, index) => {
        switch (item) {
          case "+":
            if (strArr.indexOf(arr[index + 1]) !== -1) {
              num = num.concat(`+${obj[arr[0]]}`);
            }
            num = num.concat(`+${obj[arr[0]]}`);
            arr.splice(0, 1);
            break;
          case "-":
            num = num.concat(`-${obj[arr[0]]}`);
            arr.splice(0, 1);
            break;
          case "*":
            num = num.concat(`${obj[arr[0]]} * ${obj[arr[1]]}`);
            arr.splice(0, 2);
            break;
          case "/":
            num = num.concat(`${obj[arr[0]]} / ${obj[arr[1]]}`);
            arr.splice(0, 2);
            break;
          case "%":
            num = num.concat(`${obj[arr[0]]} % ${obj[arr[1]]}`);
            arr.splice(0, 2);
          case ")":
            console.log(num);
            num = num.concat(`${obj[arr[0]]})`);
            arr.splice(0, 1);
          case "(":
            num = num.concat(`${obj[arr[0]]})`);
            arr.splice(0, 1);
            break;
        }
      });
      console.log(String(num));

      obj = {
        高度: 1,
        宽度: 2,
        度: 3,
        高: 4,
      };

      var string = "-高度*高-度-(-高/-宽度%6-9/3)";
      var newString = "";
      var newNewString = "";
      string.split("").forEach((item, index) => {
        if (ys.indexOf(item) === -1) {
          newString += item;
        } else {
          Object.keys(obj).some((objItem) => {
            if (newString === objItem) {
              newString = obj[objItem];
              newNewString += newString;
              newString = "";
              return true;
            }
          });
          if (newString) {
            newNewString += newString; // 当不是常量也不是公式符号时
          }
          newNewString += item;
          newString = "";
        }
      });
    </script>
  </body>
</html>
